/**
 * 
 */
package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.Filter;
import org.ediba.siri.core.model.domain.Model;
import org.ediba.siri.core.model.domain.ModelSchool;
import org.ediba.siri.core.model.service.ModelService;
import org.springframework.web.bind.annotation.ResponseBody;

public class ModelSchoolController extends Controller {

	private ModelService modelService;
	public void setModelService(ModelService modelService) {this.modelService = modelService;}
	
	@ResponseBody
	public Map<String,Object> postHandler(HttpServletRequest request, HttpServletResponse response, ModelSchoolCommand command){

		String name = command.getName();
		
		ModelSchool modelSchool = this.modelService.modelSchoolCreate(name);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("data", modelSchool);
		_result.put("success", true);
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ModelSchoolCommand command){
		List<ModelSchool> modelSchool = new ArrayList<ModelSchool>();
		//construccion del filtro
		Map<Filter,Collection<Object>> _filters = new HashMap<Filter, Collection<Object>>(5);
		Collection<Object> _filter;

		if (command.getIdModel() != null){
			Model model = this.modelService.retrieve(command.getIdModel());
			modelSchool.add(model.getModelschool());
		}
		else {
			if (command.getQuery() != null){
				if (command.getQuery().length > 0){
					_filter = new ArrayList<Object>(1);
					_filter.add(command.getQuery()[0]);
					_filters.put(Filter.NAME, _filter);
					modelSchool.addAll(this.modelService.modelSchoolRetrieve(_filters, command.getSort(), command.getDir()));
				}
			}
			else
				modelSchool.addAll(this.modelService.modelSchoolRetrieve());
		}
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", modelSchool);
		_result.put("total", modelSchool.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> putHandler(HttpServletRequest request, HttpServletResponse response, ModelSchoolCommand command){
		
		return null;
	}
	
	@ResponseBody
	public Map<String,Object> deleteHandler(HttpServletRequest request, HttpServletResponse response, ModelSchoolCommand command){

		return null;
	}
}
